package com.atguigu.flink.chapter07.timer;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lzc
 * @Date 2022/7/8 10:08
 */
public class Flink03_Timer_Practice {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        // 当水位超过10, 5s发出红色预警
        env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(
                    data[0],
                    Long.valueOf(data[1]),
                    Integer.valueOf(data[2])
                );
            })
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<WaterSensor>forMonotonousTimestamps()
                    .withTimestampAssigner((ws, ts) -> ws.getTs())
            )
            .keyBy(WaterSensor::getId)
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                private long timerTs;
                boolean isFirst = true;
                Integer lastVc;
    
                @Override
                public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                    out.collect("水位连续5s上升, 发出红色预警....");
                    isFirst = true;
                }
    
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    if (isFirst) {
                        isFirst = false;
                        
                        // 如果是第一条数据进来, 注册定时器
                        timerTs = value.getTs() + 5000;
                        ctx.timerService().registerEventTimeTimer(timerTs);
                        System.out.println("第一条数据进来, 注册定时器:" + timerTs);
                        
                    }else{
                        // 判断是否上升
                        if (value.getVc() < lastVc) {
                            System.out.println("水位下降, 取消定时器:" + timerTs);
                            ctx.timerService().deleteEventTimeTimer(timerTs);
                            
                            // 重新注册新的定时器
                            timerTs = value.getTs() + 5000;
                            System.out.println("重新注册定时器:" + timerTs);
                            ctx.timerService().registerEventTimeTimer(timerTs);
                        }else if (value.getVc() > lastVc){
                            System.out.println("水位上升, 什么都不坐");
                        }
                    }
    
                    lastVc = value.getVc();
                
                }
            })
            .print();
           
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
